import 'dart:async';
import 'package:flutter/material.dart';
import 'package:metaera_flutter/utils/Device.dart';
import 'package:metaera_flutter/utils/color.dart';
import 'package:cached_network_image/cached_network_image.dart'; // 添加导入

class BannerSlider extends StatefulWidget {
  final List<Map> images;

  const BannerSlider({Key? key, required this.images}) : super(key: key);

  @override
  _BannerSliderState createState() => _BannerSliderState();
}

class _BannerSliderState extends State<BannerSlider> {
  late PageController _pageController;
  late Timer _timer;
  int _currentPage = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
    // 每5秒自动切换
    _timer = Timer.periodic(Duration(seconds: 5), (Timer timer) {
      if (_currentPage < widget.images.length - 1) {
        _currentPage++;
      } else {
        _currentPage = 0;
      }

      _pageController.animateToPage(
        _currentPage,
        duration: Duration(milliseconds: 300),
        curve: Curves.easeIn,
      );
    });
  }

  @override
  void dispose() {
    _pageController.dispose();
    _timer.cancel(); // 取消定时器
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: DeviceUtils.adaptiveSizeByScreenUtil(139),
      child: Column(
        children: [
          Expanded(
            child: PageView.builder(
              controller: _pageController,
              itemCount: widget.images.length,
              itemBuilder: (context, index) {
                return Container(
                  height: DeviceUtils.adaptiveSizeByScreenUtil(139),
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: NetworkImage(widget.images[index]['pic']),
                      fit: BoxFit.cover,
                    ),
                    borderRadius: BorderRadius.circular(DeviceUtils.width8),
                  ),
                  margin: EdgeInsets.only(
                    left: DeviceUtils.width16,
                    right: DeviceUtils.width16,
                    top: DeviceUtils.width4,
                  ),
                );
              },
              onPageChanged: (int index) {
                setState(() {
                  _currentPage = index;
                });
              },
            ),
          ),
          Container(
            height: DeviceUtils.width16,
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(
                widget.images.length,
                (index) => Container(
                  width: index == _currentPage ? 12 : 6,
                  height: 6,
                  margin: EdgeInsets.symmetric(horizontal: 2),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: index == _currentPage
                        ? AppColors.color6184DD
                        : AppColors.colorA0A0A9,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
